<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PostResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        
        $user = \Auth::user();

        if($this->user->immune ==date('Ymd') || $this->user->immune ==date('Ym') || $this->user->immune == date('Y')|| $this->user->immune == -1){
            $this->user->hasimmune = true;
        }else{
            $this->user->hasimmune = false;
        }
        
        $strw = mb_strwidth(trim($this->body), 'UTF-8');


        $this->user->name = $this->user->name?$this->user->name:'游客'.$this->user->id;
        $this->user->avatar = $this->user->avatar?$this->user->avatar:'';

        $body = $strw>300?str_limit( trim($this->body),190 ): trim($this->body);
        $allbody = $strw>300?trim($this->body):'';
        $showall = $strw>300?1:0;


        return [
            'id'   => (string)$this->id,
            'user_id' => $this->user_id,
            'user' => $this->user,
            'category' => $this->category,
            'body' => $body,
            'allbody' =>$allbody ,
            'showall' =>$showall,
            'isshowall'=>0,
            'rank'  => $this->rank,
            'images'  => $this->images && $this->images[0]?$this->images:[],
            'liked'  => $this->liked, //点赞数
            'userliked'=> config('cms.allow_interaction') && $user && $user->hasLiked($this->resource), // todo待优化(访问速度很慢)
            'allow_interaction'=>config('cms.allow_interaction'), // 加载互动版块
            'commented'  => config('cms.use_comment_component')?$this->commented:-1, //评论数
            'share_title'=>$this->user->name.'',
            'share_cover'=> $this->images && $this->images[0]?$this->images[0]:'',
            'wxto'  => '/pages/article/info?id='.$this->id, // 后面建议把视频给分拆开
            'pageview'=>$this->pageview,
            'time' => $this->created_at->from(),
        ];
    }
}
